<?php

/**
 * This is the model class for table "reequation".
 *
 * The followings are the available columns in table 'reequation':
 * @property string $id
 * @property string $uid
 * @property integer $first
 * @property integer $operator
 * @property integer $second
 * @property integer $right_time
 * @property string $end_date
 */
class Reequation extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Reequation the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{reequation}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('uid, first, operator, second, add_date', 'required'),
			array('first, operator, second, right_time', 'numerical', 'integerOnly'=>true),
			array('uid', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, uid, first, operator, second, right_time, end_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'uid' => 'Uid',
			'first' => 'First',
			'operator' => 'Operator',
			'second' => 'Second',
			'right_time' => 'Right Time',
			'end_date' => 'End Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('uid',$this->uid,true);
		$criteria->compare('first',$this->first);
		$criteria->compare('operator',$this->operator);
		$criteria->compare('second',$this->second);
		$criteria->compare('right_time',$this->right_time);
		$criteria->compare('end_date',$this->end_date,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 
	 * Get a Reequation from DB 
	 * @param int $type   (1=>today || 0=>all)
	 * @param int $max the max rand value
	 * return 1 Reequation one times
	 */
	public function getUserReequation($type = 0, $max=0)
	{
	    $criteria = new CDbCriteria();
	    $criteria->condition = ' uid = :uid and end_date is null ';
	    $criteria->params[':uid'] = Yii::app()->user->id;

	    if ($type == 1){
	        $criteria->condition .= ' and add_date > :today_date ';
	        $criteria->params[':today_date'] = date('Y-m-d');
	    }
	    
	    $criteria->order = ' id DESC ';
	    
	    $criteria->limit = 1;
	    $criteria->offset = rand(0, $max-1);
	    
	    $reEquationResult = Reequation::model()->find($criteria);
//	    var_dump($reEquationResult['id']);
//	    print_r($reEquationResult);
	    return $reEquationResult;
	}
}